package server;

import java.awt.Color;
import java.awt.GradientPaint;
import java.io.File;
import java.io.IOException;
import java.util.Vector;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class Statistics {


	public static void generateStat1(Vector<Integer> values, String name) {

        DefaultCategoryDataset dataset= new DefaultCategoryDataset();
        for (int i=0; i< values.size(); i++){
        		dataset.setValue(values.get(i), "*", (i+1)+"");
        }

        // Generate the graph
        JFreeChart chart = ChartFactory.createBarChart("A Month Back Resolution For "+name, // Title
                "Days Before Today", // x-axis Label
                "Messages", // y-axis Label
                dataset, // Dataset
                PlotOrientation.VERTICAL, // Plot Orientation
                false, // Show Legend
                true, // Use tooltips
                false // Configure chart to generate URLs?
            );

              GradientPaint gradientpaint0 = new GradientPaint(0.0F, 0.0F, new Color(230, 230, 250), 0.0F, 0.0F, new Color(136, 136, 255));   
             GradientPaint gradientpaint1 = new GradientPaint(0.0F, 0.0F, new Color(240, 255, 240), 0.0F, 0.0F, new Color(136, 136, 255));  
             GradientPaint gradientpaint2 = new GradientPaint(0.0F, 0.0F, new Color(205, 92, 92), 0.0F, 0.0F, new Color(136, 136, 255));  
             BarRenderer r = (BarRenderer) chart.getCategoryPlot().getRenderer();  
                 r.setSeriesPaint(0, gradientpaint0);  
                 r.setSeriesPaint(1, gradientpaint1);  
                 r.setSeriesPaint(2, gradientpaint2);   

        try {
            ChartUtilities.saveChartAsJPEG(new File("web\\html\\stat1"+name+".jpg"), chart, 700,
                450);
        } catch (IOException e) {
            System.err.println("Problem occurred while creating chart!!");
        }
	}



	/**
	 * @param args
	 */
	public static void generateStat2(Vector<Integer> values,int daysPassed){

        XYSeries series = new XYSeries("XYGraph");

        for (int i=0; i< values.size(); i++){
        	series.add(i, values.get(i));
        }

        XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(series);
        // Generate the graph
        JFreeChart chart = ChartFactory.createXYLineChart("Average Statistics Of Messages During 24 hrs", // Title
                "Hour", // x-axis Label
                "Messages", // y-axis Label
                dataset, // Dataset
                PlotOrientation.VERTICAL, // Plot Orientation
                false, // Show Legend
                true, // Use tooltips
                false // Configure chart to generate URLs?
            );
     // x axis: the range is known in advance
        NumberAxis domain = new NumberAxis("Hour");
        domain.setAutoTickUnitSelection(false) ;
        domain.setRange(0, 23) ;
        chart.getXYPlot().setDomainAxis(domain) ;

        // y axis: the range is only known to start from 0
        NumberAxis range = new NumberAxis("Messages") ;
        range.setAutoTickUnitSelection(false) ;
        range.setTickUnit(new NumberTickUnit(1)) ;
        chart.getXYPlot().setRangeAxis(range) ;
        chart.getXYPlot().getRangeAxis().setLowerBound(0) ;
        try {
        	ChartUtilities.saveChartAsJPEG(new File("web\\html\\stat2.jpg"), chart, 700,
                450);
        } catch (IOException e) {
            System.err.println("Problem occurred while creating chart!!");
        }
}

	public static void generateStat3(Vector<Integer> values, int daysPassed) {

        DefaultCategoryDataset dataset= new DefaultCategoryDataset();
        for (int i=0; i< values.size(); i++){
        	if (i<12)
        		dataset.setValue(values.get(i)/daysPassed, "*", i+"");
        	else
        		dataset.setValue(values.get(i)/daysPassed, "*", i+"");

        }

        // Generate the graph
        JFreeChart chart = ChartFactory.createBarChart("A 24 hrs Resoloution - Online Users", // Title
                "Hour", // x-axis Label
                "Active Users", // y-axis Label
                dataset, // Dataset
                PlotOrientation.VERTICAL, // Plot Orientation
                false, // Show Legend
                true, // Use tooltips
                false // Configure chart to generate URLs?
            );
        try {
            ChartUtilities.saveChartAsJPEG(new File("web\\html\\stat3.jpg"), chart, 700,
                450);
        } catch (IOException e) {
            System.err.println("Problem occurred while creating chart!!");
        }
	}

}
